import { Toast } from 'mint-ui'

export default {
  // 获取sessionStorage
  getSession (key) {
    return JSON.parse(window.localStorage.getItem(key))
  },
  // 设置sessionStorage
  setSession (key, data) {
    return window.localStorage.setItem(key, JSON.stringify(data))
  },
  // 时间戳转时间
  dateParse (timestamp) {
    if (timestamp) {
      let date = new Date(timestamp)
      let Y = date.getFullYear()
      let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
      let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      let h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
      let m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
      let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
      return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s
    } else {
      return ''
    }
  },
  // 从url中获取参数
  getRequest () {
    let url = window.location.href
    let theRequest = {}
    if (url.indexOf('?') !== -1) {
      let str = url.split('?')[1]
      let strs = str.split('&')
      for (let i = 0; i < strs.length; i++) {
        theRequest[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1])
      }
    }
    return theRequest
  },
  // Toast
  toast (message, time) {
    // 默认提示时间1s
    let arr1 = document.querySelectorAll('.mint-toast')
    let showMessage = true
    if (arr1.length > 0) {
      for (let i = 0, length = arr1.length; i < length; i++) {
        if (arr1[i].innerText === message) {
          showMessage = false
        } else {
          arr1[i].parentElement.removeChild(arr1[i])
        }
      }
    }
    if (showMessage && message) {
      Toast({
        message: message,
        duration: time || 1000
      })
    }
  },
  getBaseUrl () {
    let baseUrl = location.href.split('#')[0]
    if (baseUrl.indexOf('http://123.207.169.200:8080') === -1) {
      return 'http://123.207.169.200:8080'
    } else {
      return 'http://123.207.169.200:8080'
    }
  }
}
